import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
from sklearn.datasets import load_boston

# 导入波士顿房价数据集
boston = load_boston()
X = boston.data
y = boston.target

# 划分数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 特征标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# 不同核函数的SVR模型
kernels = ['linear', 'rbf']  # 尝试线性核和径向基核

for kernel in kernels:
    # 创建SVR模型
    svr = SVR(kernel=kernel)

    # 拟合模型
    svr.fit(X_train_scaled, y_train)

    # 在训练集和测试集上进行预测
    y_train_pred = svr.predict(X_train_scaled)
    y_test_pred = svr.predict(X_test_scaled)

    # 评估模型性能
    mse_train = mean_squared_error(y_train, y_train_pred)
    mse_test = mean_squared_error(y_test, y_test_pred)

    # 打印结果
    print(f"Kernel: {kernel}")
    print(f"Mean Squared Error on Training Set: {mse_train:.2f}")
    print(f"Mean Squared Error on Test Set: {mse_test:.2f}")

    # 绘制决策边界
    plt.figure(figsize=(10, 6))
    plt.scatter(y_test, y_test_pred, color='blue')
    plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], linestyle='--', color='red', linewidth=2)
    plt.title(f'{kernel} Kernel - Support Vector Regression')
    plt.xlabel('True Values')
    plt.ylabel('Predictions')
    plt.show()
